package DB;

import Communication.MessageHandle;
import Communication.Client;
import Entity.Customer;

/** this class asks queries to get and insert customer's details from and into the db  */
public class CustomerQuery {

	public String [] CityList=new String[256];
	
	/**function which insert customer's details into db 
	 * 
	 * @param Cst
	 * @return
	 */
	public static String InsertCustomerQuery(Customer Cst) 
	{
		return  "INSERT INTO customers (CustomerID , firstName, lastName, Age,City, PhoneNumber, PurchaseCounter, Priority) VALUES ("
				+ Cst.getCustomerID()
				+ ",\""
				+ Cst.getFirstName()
				+ "\",\""
				+ Cst.getLastName()
				+ "\",\""
				+ Cst.getAge()
				+ "\",\""
				+ Cst.getCity()
				+ "\",\""
				+ Cst.getPhoneNumber()
				+ "\",\""
				+ Cst.getPurchaseCounter()
				+ "\",\""
				+ Cst.getPriority()
				+ "\")";
	}

	/**function which insert customer's details into db 
	 * 
	 * @param client
	 * @param Cst
	 */
	public void InsertCustomer(Client client, Customer Cst) 
	{
		String sql = "INSERT INTO customers (CustomerID , firstName, lastName, Age,City, PhoneNumber, PurchaseCounter, Priority) VALUES ("
				+ Cst.getCustomerID()
				+ ",\""
				+ Cst.getFirstName()
				+ "\",\""
				+ Cst.getLastName()
				+ "\",\""
				+ Cst.getAge()
				+ "\",\""
				+ Cst.getCity()
				+ "\",\""
				+ Cst.getPhoneNumber()
				+ "\",\""
				+ Cst.getPurchaseCounter()
				+ "\",\""
				+ Cst.getPriority()
				+ "\")";

		client.getData(sql, MessageHandle.CUSTOMER, true);
	}
	
	/**function which get all selected id's customer from db */
	public static String getCustomerQuery(String customerID) 
	{
		return "SELECT * FROM customers WHERE CustomerID = " + "\"" + customerID + "\"";
	}

	/**function which get cities list from db*/
	public static String getCities() 
	{
		return "SELECT * FROM cities" ;
	}
	
	/**functions which check if a field is empty */
	public boolean isEmpty(String field) 
	{
		if (field.isEmpty())
			return true;
		return false;
	}

	public boolean isEmpty(int field) 
	{
		if (field != 0)
			return true;
		return false;
	}
	
	/**function which update customer's db */
	public static String updateCustomer(Customer customer) 
	{
		String sql = "UPDATE `customers` SET `firstName`='"
				+ customer.getFirstName() + "', `lastName`='"
				+ customer.getLastName() + "', `City`='" + customer.getCity()
				+ "', `PhoneNumber`='" + customer.getPhoneNumber()
				+ "' WHERE `CustomerID`='" + customer.getCustomerID() + "'";
		return sql;
	}
}
		

